package com.hbnu.system.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.TranprojectfileMapper;
import com.hbnu.system.model.entity.Tranproject_registration;
import com.hbnu.system.model.entity.Tranprojectfile;
import com.hbnu.system.service.ITranproject_registrationService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.service.TranprojectfileService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.BindException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * (Tranprojectfile)表服务实现类
 *
 * @author makejava
 * @since 2023-10-16 20:45:33
 */
@Service("tranprojectfileService")
public class TranprojectfileServiceImpl extends ServiceImpl<TranprojectfileMapper, Tranprojectfile> implements TranprojectfileService {
    @Autowired
    FileUploadUtil fileUploadUtil;

    @Autowired
    ITranproject_registrationService iTranproject_registrationService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private CheckRoleUtil checkRoleUtil;


    /**
     * 添加附件
     * @param tranprojectfile
     * @return
     */
    @Override
    public boolean addTranprojectfile(Tranprojectfile tranprojectfile) {
        tranprojectfile.setFiledate(LocalDateTime.now());
        Tranproject_registration tranProjectRegistration = iTranproject_registrationService.getById(tranprojectfile.getTregistrationID());
        checkRoleUtil.checkPermissionProject(tranProjectRegistration.getProjectState());

        if(baseMapper.insert(tranprojectfile) != 1) {
            throw new BaseException("上传失败,请重新尝试");
        }
        return true;
    }


    @Override
    public List<Tranprojectfile> pageTranprojectfile(String id) {
        if(StrUtil.isBlank(id)){
            throw new BaseException("横向项目编号为空值");
        }
        List<Tranprojectfile> tranprojectfile = baseMapper.selectList(Wrappers.lambdaQuery(Tranprojectfile.class)
                .eq(Tranprojectfile::getTregistrationID,id));
        return tranprojectfile;
    }

    /**
     * 删除附件
     * @param id
     * @return
     * @throws IOException
     */
    @Override
    public boolean deletTranprojectfile(String id) throws IOException {
        Tranprojectfile one = super.getOne(Wrappers.lambdaQuery(Tranprojectfile.class)
                .eq(Tranprojectfile::getFileid, id));
        if (Objects.isNull(one)){
            throw new BaseException("文件编号有误!");
        }
        Tranproject_registration project = iTranproject_registrationService.getTranproject(one.getTregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());
        boolean delete = fileUploadUtil.delete(one.getFileurl());
        if(!delete){
            throw new BindException("附件删除失败");
        }

        return baseMapper.deleteById(id) == 1;
    }

    /**
     * 修改附件
     * @param tranprojectfile
     * @return
     */
    @Override
    public boolean updateTranprojectfile(Tranprojectfile tranprojectfile) {
        Tranproject_registration project = iTranproject_registrationService.getTranproject(tranprojectfile.getTregistrationID());
        checkRoleUtil.checkPermissionProject(project.getProjectState());
        return baseMapper.updateById(tranprojectfile)==1;
    }


}
